import logging.handlers
import os
import time

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_URL = "http://user-p2p-test.itheima.net"
#初始化日志配置
def init_log_config():
    #1.初始化日志对象
    logger = logging.getLogger()
    #2.设置日志级别
    logger.setLevel(logging.INFO)
    #3.创建控制台处理器和文件日志处理器
    sh = logging.StreamHandler()

    logfile = BASE_DIR + os.sep + "log"+os.sep + "p2plog{}.log".format(time.strftime("%Y_%m_%d %H_%M_%S"))
    fh = logging.handlers.TimedRotatingFileHandler(logfile,
                                                   when='M',
                                                   interval=5,
                                                   backupCount=5,
                                                   encoding="utf-8")
    #4.设置日志格式 创建格式化器
    fmt = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'
    fm = logging.Formatter(fmt)
    #5.将格式器设置到日志器中
    sh.setFormatter(fm)
    fh.setFormatter(fm)
    #6.将日志处理器添加到日志对象
    logger.addHandler(sh)
    logger.addHandler(fh)


